#include "GraphicalElement.h"
#include <algorithm>
#include "NoAnimation.h"

GraphicalElement::GraphicalElement()
        : id(0), texture(NULL), position(0, 0)
{
    setAnimation(new NoAnimation());
}

GraphicalElement::GraphicalElement(long id)
        : id(id), texture(NULL), position(0, 0)
{
    setAnimation(new NoAnimation());
}

GraphicalElement::~GraphicalElement()
{
    delete animation;
}

void GraphicalElement::traverse(const Event& event)
{
    handleEvent(event);
}

void GraphicalElement::setTexture(Texture* newTexture)
{
    texture = newTexture;
}

void GraphicalElement::setPosition(const Position& newPosition)
{
    position = newPosition;
}

Position GraphicalElement::getPosition()
{
    return position;
}

void GraphicalElement::deleteTexture()
{
    delete texture;
    texture = NULL;
}

void GraphicalElement::update(unsigned long tick)
{
    if (animation != NULL)
    {
        animation->animate(tick);
    }
}

void GraphicalElement::setAnimation(Animation* newAnimation)
{
    animation = newAnimation;
    animation->setElement(this);
    animation->init();
}

Texture* GraphicalElement::getTexture()
{
    return texture;
}

void GraphicalElement::handleEvent(const Event& event)
{
}

GraphicalElement* GraphicalElement::findChild(long idToFind)
{
    if (idToFind == id)
    {
        return this;
    }
    return NULL;
}

void GraphicalElement::render(const Position& parent)
{
    animation->render(parent);
    if (animation->done())
    {
        delete animation;
        setAnimation(new NoAnimation());
    }
}

long GraphicalElement::getId()
{
    return id;
}
